package com.example.musicplayer.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.example.musicplayer.R;
import com.example.musicplayer.adapter.MusicListAdapter;
import com.example.musicplayer.entity.MusicList;
import com.example.musicplayer.provider.MusicListProvider;
import com.example.musicplayer.util.DbUtil;

import java.util.List;


public class AddListDialogFragment extends DialogFragment {

    private EditText mInputListName;
    private MusicListAdapter adapter;
    private DbUtil mDbUtil = new DbUtil();
    private List<MusicList> mMusicList;


    public AddListDialogFragment(MusicListAdapter adapter, List<MusicList> mMusicList) {
        this.adapter = adapter;
        this.mMusicList = mMusicList;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View view = inflater.inflate(R.layout.add_list_dialog, null);
        mInputListName = view.findViewById(R.id.input_list_name);
        builder.setView(view)
                // Add action buttons
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // sign in the user ...
                        ContentValues values = new ContentValues();
                        values.put(MusicList.MUSIC_LIST_NAME, mInputListName.getText().toString());
                        if (getContext() != null && getActivity() != null) {
                            Uri newUri = getActivity().getContentResolver().insert(MusicListProvider.CONTENT_URI, values);
                            int musicListId = mDbUtil.getMusicListIdByName(getContext(), mInputListName.getText().toString());
                            int musicListCount = mDbUtil.getMusicCountOfMusicList(getContext(), mInputListName.getText().toString());
                            MusicList musicList = new MusicList(musicListId, mInputListName.getText().toString(), musicListCount);
                            adapter.addData(mMusicList.size(), musicList);
                            Toast.makeText(getContext(), "添加成功，URI" + newUri, Toast.LENGTH_SHORT).show();
                        } else {
                            Log.i("===============", "添加出错");
                        }

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dismiss(); // 关闭对话框
                    }
                });
        return builder.create();
    }
}
